package lol.clann.minecraft.springboot.plugin.core.database.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import com.mysql.jdbc.Driver;
import lol.clann.minecraft.springboot.api.annotation.condition.ConditionalOnConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * 配置数据源
 *
 *
 */
@Slf4j
@Configuration
@ConditionalOnConfig(value = "springbootplugin.datasource.enable", havingValue = "true", matchIfMissing = true)
public class ImportDruidSource {

    @Primary
    @Bean
    public DruidDataSource dataSource(@Autowired DataSourceConfig dataSourceConfig) throws SQLException {
        DruidDataSource datasource = new DruidDataSource();
        if (dataSourceConfig.isUseMysql()) {
            LOGGER.info("database:mysql");
            datasource.setDriverClassName(Driver.class.getName());
            datasource.setUrl(dataSourceConfig.getDbUrl());
            datasource.setUsername(dataSourceConfig.getUsername());
            datasource.setPassword(dataSourceConfig.getPassword());
        } else {
            LOGGER.info("database:h2");
//            直接指定 h2BindAddress
            System.setProperty("h2.bindAddress", dataSourceConfig.getH2BindAddress());
            datasource.setDriverClassName(relocate.org.h2.Driver.class.getName());
            String url = dataSourceConfig.getH2Url().replace("${dir}", System.getProperty("user.dir"));
            datasource.setUrl(url);
        }
        datasource.setInitialSize(dataSourceConfig.getInitialSize());
        datasource.setMinIdle(dataSourceConfig.getMinIdle());
        datasource.setMaxActive(dataSourceConfig.getMaxActive());
        datasource.setMaxWait(dataSourceConfig.getMaxWait());
        datasource.setTimeBetweenEvictionRunsMillis(dataSourceConfig.getTimeBetweenEvictionRunsMillis());
        datasource.setMinEvictableIdleTimeMillis(dataSourceConfig.getMinEvictableIdleTimeMillis());
        datasource.setValidationQuery(dataSourceConfig.getValidationQuery());
        datasource.setTestWhileIdle(dataSourceConfig.isTestWhileIdle());
        datasource.setTestOnBorrow(dataSourceConfig.isTestOnBorrow());
        datasource.setTestOnReturn(dataSourceConfig.isTestOnReturn());
        datasource.setPoolPreparedStatements(dataSourceConfig.isPoolPreparedStatements());
        return datasource;
    }

    /**
     * 配置事物管理
     *
     * @param dataSource
     * @return
     * @throws SQLException
     */
    @Bean(name = "transactionManager")
    @Primary
    public static DataSourceTransactionManager masterTransactionManager(@Qualifier("dataSource") DataSource dataSource) {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }
}